﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Photon.SocketServer;
using Common;
using Common.Tools;
using MyGameServer.Manager;


namespace MyGameServer.Handler
{
    class ChatHandler:BaseHandler
    {
        public ChatHandler()
        {
            OpCode = OperationCode.Chat;
        }
        public override void OnOperationRequest(OperationRequest operationRequest, SendParameters sendParameters, ClientPeer peer)
        {
            string content = (string)DictTool.GetValue<byte, object>(operationRequest.Parameters, (byte)ParameterCode.Chat);
            
            OperationResponse response = new OperationResponse(operationRequest.OperationCode);
            Dictionary<byte, object> data = new Dictionary<byte, object>();
            data.Add((byte)ParameterCode.Chat, content);

            response.Parameters = data;
            response.ReturnCode = (short)Common.ReturnCode.Success;
            
            peer.SendOperationResponse(response, sendParameters);
            
            MyGameServer.log.Info("聊天成功: " + content);



            foreach (var tempPeer in MyGameServer.Instance.peerList)
            {
                if (string.IsNullOrEmpty(tempPeer.username) == false)
                {
                    EventData ed = new EventData((byte)EventCode.Chat);
                    //Dictionary<byte, object> data2 = new Dictionary<byte, object>();
                    //data2.Add((byte)ParameterCode.Username, peer.username);
                    ed.Parameters = data;
                    tempPeer.SendEvent(ed, sendParameters);
                }
            }
        }
    }
}
